<?php

namespace eu\freeplace\page\model;

use eu\freeplace\page\model\ValueObject as ValueObject;

class TemplateVO extends ValueObject {

    const CLASS_NAME = "TemplateVO";
    const DB_TABLE   = "template";

    private $DocumentType = null;
    private $ViewType     = null;
    private $Template     = null;

    public function __construct($documentType, $viewType) {
        global $logger;
        $logger->log("Calling instance of ".self::CLASS_NAME."( DocumentType:".$documentType.", ViewType:".$viewType.")", "NOTICE");

        try {
            if (strlen($documentType) < 1)
                throw new Exception(self::CLASS_NAME . " - could not fetch Template ".$documentType."[".$viewType."]");

            $this->DocumentType = $documentType;
            $this->ViewType     = $viewType;
        } catch (Exception $e) {

            $logger->log($e->getMessage(), "NOTICE");
        }
        return null;
    }

    public function __clone() {
        self::fetchTemplate($this->DocumentType, $this->ViewType);
    }

    public function __toString() {
        return "{/**TODO: implement me */}";
    }

    protected function createDatabaseTable() {
        $sql =
        "CREATE TABLE IF NOT EXISTS ".$this->getTablePrefix().self::DB_TABLE." (
            DocumentType CHAR(255) NOT NULL,
            ViewType     CHAR(255) NOT NULL default 'default',
            Template     TEXT,

            PRIMARY KEY(DocumentType, ViewType)
        );";
        return $sql;
    }

    public function getClassAttributes() {
        $template['DocumentType'] = $this->DocumentType;
        $template['ViewType']     = $this->ViewType;
        $template['Template']     = $this->Template;
        return $template;
    }

    private function fetchTemplate($documentType, $viewType) {
        global $logger;

        if($viewType == "") $viewType = "default";

        $table_name = $this->getTablePrefix().self::DB_TABLE;
        $sql = "SELECT * FROM ".$table_name." WHERE DocumentType='".$documentType."' AND ViewType='".$viewType."';";

        $logger->log(self::CLASS_NAME."().fetchTemplate()", "DEBUG");
        $logger->log("\t".$sql, "DEBUG");

        $pdo = $this->getResult($sql);
        $result = $pdo->fetch();

        if($result != 0) {
            $this->DocumentType = $result['DocumentType'];
            $this->ViewType     = $result['ViewType'];
            $this->Template     = trim($result['Template']);
        } else {
            $logger->log("\tCould not fetch a template.", "DEBUG");
        }
    }

    ### [TODO]##################################################################

    /**
     * Count all existing Templates (include the ViewTypes) in the Database
     */
    public function countAllTemplates() {}

    /**
     * Return a list of Templates-Names from a DocumentType Group.
     */
    public function getTemplatesByDocumentType($documentType) {return $documentType;}

    /**
     * Return a complete List of all the Template Names in the Database.
     */
    public function getAllDocumetTypes() {}

}//CLASS

?>


